package com.project.fiveCerealsSavoury.service.impl;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.project.fiveCerealsSavoury.domain.DeliveryDomain;
import com.project.fiveCerealsSavoury.domain.Goods;
import com.project.fiveCerealsSavoury.domain.LogisticsDomain;
import com.project.fiveCerealsSavoury.domain.LogitsticsState;
import com.project.fiveCerealsSavoury.domain.Order;
import com.project.fiveCerealsSavoury.domain.OrderState;
import com.project.fiveCerealsSavoury.domain.ReportForms;
import com.project.fiveCerealsSavoury.domain.Trade;
import com.project.fiveCerealsSavoury.domain.TradeStatus;
import com.project.fiveCerealsSavoury.exception.BizException;
import com.project.fiveCerealsSavoury.service.AdministratorService;
import com.project.fiveCerealsSavoury.service.DeliveryService;
import com.project.fiveCerealsSavoury.service.GoodsService;
import com.project.fiveCerealsSavoury.service.LogisticsService;
import com.project.fiveCerealsSavoury.service.OrderService;
import com.project.fiveCerealsSavoury.service.ReportFormsService;
import com.project.fiveCerealsSavoury.service.TradeService;
import com.project.fiveCerealsSavoury.service.UserService;
import com.project.fiveCerealsSavoury.transfer.Report;

@Service
@Transactional
public class AdministratorServiceImpl implements AdministratorService {
	@Autowired
	private GoodsService goodsService;
	@Autowired
	private TradeService tradeService;
	@Autowired
	private ReportFormsService reportFormsService;
	@Autowired
	private LogisticsService logisticsService;
	@Autowired
	private OrderService orderService;
	@Autowired
	private DeliveryService deliveryService;
	@Autowired
	private UserService userService;
	/**
	 * 查询记录
	 * 
	 * @param tradeStatus
	 * @return
	 */
	@Override
	public List<Trade> getCompany(TradeStatus tradeStatus)throws BizException {
		List<Trade> listStatus = tradeService.listStatus(tradeStatus);
		Map<Integer, Report> map = new LinkedHashMap<>();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd  hh-mm-ss");
		SimpleDateFormat formatters = new SimpleDateFormat("yyyy-MM");
		Double tmp = 0.0;
		Double bal = 0.0;
		Double del = 0.0;
		boolean isNot = false;
		for(int i = 1; i <= 12;i++){
			Report report = new Report();
			DeliveryDomain deliveryDomain = null;
			for (Trade trade : listStatus) {
				//分开查询每月的交易记录
				String format = formatter.format(trade.getTime());
				String formats = formatters.format(trade.getTime());
				int indexOf = format.indexOf("-");
				String substring = format.substring(indexOf+1, indexOf+2);//2017-08-12--->0
				String substrings = format.substring(indexOf+1, indexOf+3);//2017-08-12--->08
				List<ReportForms> listTime = null;
				try {
					listTime = reportFormsService.listTime(formatters.parse(formats));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if (listTime == null || listTime.isEmpty()) {
					isNot = true;
					Order queryById = orderService.queryById(trade.getOrderId(), OrderState.已完成);
					if (queryById != null) {
						if (substring.equals("0")) {//判断是否为0
							String substring1 = format.substring(indexOf+2, indexOf+3);//8和i是否相等
							
							if (substring1.equals(String.valueOf(i))) {
								tmp += trade.getWeight();
								bal += trade.getTradeMoney();
								deliveryDomain = deliveryService.query(queryById.getId());
								if (deliveryDomain.getSalary() != null) {
									del+=deliveryDomain.getSalary();
								}
								report.setTime(trade.getTime());
							}
						}else{
							if (substrings.equals(String.valueOf(i))) {
								tmp += trade.getWeight();
								bal += trade.getTradeMoney();
								deliveryDomain = deliveryService.query(trade.getOrderId());
								report.setTime(trade.getTime());
							}
						}
					}
				}
				if (deliveryDomain != null) {
					//送货人
					report.setDayMoneyDelivery(del);
					//总收入
					report.setSale(bal);
					//每月的总重量
					report.setWeight(tmp);
					map.put(i, report);
				}
			}
		}
		if (isNot) {
			reportFormsService.appendRep(map);
		}
		return listStatus;
	}
	
	/**
	 * 发货
	 * 订单-->填写运单-->减库存
	 */
	@Override
	public boolean consignment(Integer orderId,Integer deliveryId,String startAddress,String waybillId ) throws BizException{
		if (orderId == null) throw new BizException("订单id不能为空");
		if (deliveryId == null)throw new BizException("送货人id不能为空");
		if (startAddress == null)throw new BizException("公司地址不能为空");
		Order queryById = orderService.queryById(orderId, OrderState.已支付);
		if (queryById == null) 
			throw new BizException("暂时没有订单");
		LogisticsDomain logisticsDomain = new LogisticsDomain();
		logisticsDomain.setWaybillId(waybillId);
		logisticsDomain.setDeliveryId(deliveryId);
		logisticsDomain.setDeliveryTime(new Timestamp(new Date().getTime()));
		logisticsDomain.setDeliverGoods(LogitsticsState.已发货);
		logisticsDomain.setName(queryById.getGoodName());
		logisticsDomain.setStartAddress(startAddress);
		logisticsDomain.setIndentId(queryById.getId());
		logisticsService.add(logisticsDomain);
		List<Goods> queryAll = goodsService.queryAll(queryById.getGoodName());
		for (Goods goods : queryAll) {
			if (goods.getCount() <= 0) {
				throw new  BizException("库存不足");
			}
		}
		orderService.modifyOrderState(queryById.getOrderNumber(), OrderState.已完成);
		int goods = goodsService.stocks(queryById.getGoodName(), -queryById.getGoodNumber());
		if (goods == 0)throw new BizException("没有这个商品");
		return true;
	}
	

	/**
	 * 处理退货订单
	 */
	@Override
	public boolean dispose(Integer orderId) throws BizException {
		Order queryById = orderService.queryById(orderId, OrderState.已取消);
		if (queryById == null) 
			throw new BizException("暂时没有订单");
		userService.fullMoney(queryById.getGoodNumber()*queryById.getGoodPrice(), queryById.getUserId());
		int goods = goodsService.stocks(queryById.getGoodName(), queryById.getGoodNumber());
		if (goods == 0)throw new BizException("没有这个商品");
		return true;
	}
	
}
